<template>
	<view class="exchange">
		<Navbar>积分兑换</Navbar>
		<scroll-view scroll-y class="scroll_view">
			<view class="content">
				<view class="item">
					<view class="key">数量</view>
					<input type="text" placeholder="请输入消耗的积分" class="value" v-model="number" @input="change">
				</view>
				<view class="number">
					<text>积分余额：{{balance}}</text>
					<text v-if="number">预计可兑换：{{total}}</text>
				</view>
				<view class="item">
					<view class="key">类型</view>
					<view class="type">
						<image :src="`${baseImageUrl}/common/gy.png`"></image>
						<text>官银</text>
					</view>
				</view>
				<view class="item">
					<view class="key">支付密码</view>
					<input type="text" password placeholder="请输入支付密码" v-model="password" class="value">
				</view>
				<view class="btn" @click="submit">确认兑换</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		userIntegralExchangeGet,
		userIntegralExchange
	} from '@/request/api.js';
	// import {
	// 	captchaAppId
	// } from '@/utils/config.js';
	export default {
		data() {
			return {
				number: "",
				balance: 0,
				total: 0,
				password: "",
				radioList: [],
				value: '',
			}
		},
		async onLoad() {
			let {
				integral,
				user_balance_field
			} = await userIntegralExchangeGet();
			this.balance = integral;
			let radioList = this.radioList = user_balance_field;
			this.value = radioList.length ? radioList[0].key : "";
		},
		methods: {
			change() {
				let radioList = this.radioList;
				let value = this.value;
				let dataItem = radioList.find(item => item.key == value);
				let rato = dataItem.rato;

				let number = this.number;
				let total = (Number(number) / Number(rato)).toFixed(2);
				this.total = total;
			},
			submit() {
				// const captcha = new TencentCaptcha(captchaAppId, async (res) => {
				// 	if (res.ret !== 0) return false;
				// 	let {
				// 		randstr,
				// 		ticket
				// 	} = res;
				uni.$u.throttle(async () => {
					await userIntegralExchange({
						number: this.number,
						exchage: this.value,
						password: this.password
					});
					this.$u.toast("操作成功");
				}, 2000);
				// }, {});
				// captcha.show();
				// uni.$u.throttle(async () => {
				// 	await userIntegralExchange({
				// 		number: this.number,
				// 		exchage: this.value,
				// 		password: this.password
				// 	});
				// 	this.$u.toast("操作成功");
				// }, 2000);
			}
		}
	}
</script>

<style scoped lang="scss">
	.exchange {
		height: 100vh;
		background: #29170D;
		overflow: hidden;

		.scroll_view {
			height: calc(100vh - #{$navbarHeight});
			padding-top: 48rpx;
			box-sizing: border-box;

			.content {
				width: 688rpx;
				height: 606rpx;
				margin: 0 auto;
				padding: 50rpx 40rpx;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/bg/688_606.png) no-repeat;
				background-size: 100% 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: center;

				.item {
					width: 100%;
					min-height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.key {
						width: 160rpx;
						text-align: right;
						color: #494338;
						font-size: 32rpx;
						font-weight: 400;
						font-family: SimSun;
						padding-right: 30rpx;
						box-sizing: border-box;
					}

					.type {
						display: flex;
						align-items: center;

						image {
							width: 40rpx;
							height: 40rpx;
							margin-right: 10rpx;
						}
					}

					.value {
						flex: 1;
						height: 80rpx;
						border-radius: 10rpx;
						padding: 0 108rpx 0 48rpx;
						box-sizing: border-box;
						background: #C0B5B6;
						box-shadow: 2px -1px 10px 0px rgba(3, 0, 0, 0.2);
						position: relative;

						view {
							width: 100%;
							height: 80rpx;
							line-height: 80rpx;
							text-align: center;
							color: rgba(73, 67, 56, 0.5);
							font-size: 28rpx;
							font-weight: 400;
							font-family: SimSun;
						}

						image {
							width: 40rpx;
							height: 40rpx;
							position: absolute;
							right: 20rpx;
							top: 20rpx;
						}
					}
				}

				.number {
					width: 100%;
					margin-top: 12rpx;
					display: flex;
					justify-content: flex-end;

					text {
						color: #494338;
						font-size: 24rpx;
						font-weight: 400;
						font-family: SimSun;

						&:last-child {
							margin-left: 30rpx;
						}
					}
				}

				.btn {
					width: 204rpx;
					height: 74rpx;
					line-height: 74rpx;
					text-align: center;
					color: #FFFFFF;
					font-size: 30rpx;
					font-weight: 400;
					font-family: SimSun;
					background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
					background-size: 100% 100%;
				}
			}
		}
	}
</style>